package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iwomy.secureplat.admin.api.entity.SysDept;
import com.iwomy.secureplat.common.data.datascope.DataScope;
import com.iwomy.secureplat.platform.dto.BasicAlarmContactsDTO;
import com.iwomy.secureplat.platform.entity.BasicAlarmContactsEntity;
import com.iwomy.secureplat.platform.entity.BasicInstallLocationEntity;
import com.iwomy.secureplat.platform.mapper.BasicInstallLocationMapper;
import com.iwomy.secureplat.platform.utils.RemoteDeptUtils;
import com.iwomy.secureplat.platform.mapper.BasicAlarmContactsMapper;
import com.iwomy.secureplat.platform.service.BasicAlarmContactsService;
import com.iwomy.secureplat.platform.vo.BasicAlarmContactsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 接警联系人
 *
 * @author pig
 * @date 2023-12-01 11:54:13
 */
@Service
public class BasicAlarmContactsServiceImpl extends ServiceImpl<BasicAlarmContactsMapper, BasicAlarmContactsEntity> implements BasicAlarmContactsService {

	@Autowired
	private RemoteDeptUtils remoteDeptUtils;

	@Autowired
	private BasicInstallLocationMapper installLocation;

	@Override
	public Page<BasicAlarmContactsVO> getBasicAlarmContactsPage(Page page, BasicAlarmContactsDTO basicAlarmContactsDTO) {
		LambdaQueryWrapper<BasicAlarmContactsEntity> queryWrapper = Wrappers.lambdaQuery();
		List<BasicAlarmContactsEntity> list = this.baseMapper.selectListByScope(queryWrapper, new DataScope());
		List<Long> alarmContactsId = list.stream().map(BasicAlarmContactsEntity::getId).collect(Collectors.toList());

		if (ObjectUtil.isEmpty(alarmContactsId)) return page;

		LambdaQueryWrapper<BasicAlarmContactsEntity> wrapper = Wrappers.lambdaQuery();

		if (StrUtil.isNotEmpty(basicAlarmContactsDTO.getName())) {
			List<Long> deptId = (List<Long>) remoteDeptUtils.getDeptId(basicAlarmContactsDTO.getName()).getData();
			if (deptId != null && !deptId.isEmpty()) {
				wrapper.and(w -> w.in(BasicAlarmContactsEntity::getDeptId, deptId)
						.or().like(BasicAlarmContactsEntity::getPhone, basicAlarmContactsDTO.getName())
						.or().like(BasicAlarmContactsEntity::getName, basicAlarmContactsDTO.getName())
						.or().like(BasicAlarmContactsEntity::getEmail, basicAlarmContactsDTO.getName()));
			} else {
				wrapper.and(w -> w.like(BasicAlarmContactsEntity::getPhone, basicAlarmContactsDTO.getName())
						.or().like(BasicAlarmContactsEntity::getName, basicAlarmContactsDTO.getName())
						.or().like(BasicAlarmContactsEntity::getEmail, basicAlarmContactsDTO.getName()));
			}
		}

		if (ObjectUtil.isNotEmpty(basicAlarmContactsDTO.getDeptLimit())) {
			if (basicAlarmContactsDTO.getDeptLimit() == 20) {
				wrapper.in(BasicAlarmContactsEntity::getDeptId, basicAlarmContactsDTO.getDeptId());
			} else {
				List<SysDept> deptList = (List<SysDept>) remoteDeptUtils.getDescendantList(basicAlarmContactsDTO.getDeptId()).getData();
				List<Long> deptIds = new ArrayList<>();
				for (SysDept dept : deptList) {
					deptIds.add(dept.getDeptId());
				}
				if (deptIds != null) {
					wrapper.in(BasicAlarmContactsEntity::getDeptId, deptIds);
				}
			}
		} else {
			if (ObjectUtil.isNotEmpty(basicAlarmContactsDTO.getDeptId())) {
				wrapper.in(BasicAlarmContactsEntity::getDeptId, basicAlarmContactsDTO.getDeptId());
			}
		}
		wrapper.in(BasicAlarmContactsEntity::getId, alarmContactsId);

		Page<BasicAlarmContactsEntity> basicAlarmContactsPage = baseMapper.selectPage(page, wrapper);
		List<BasicAlarmContactsEntity> records = basicAlarmContactsPage.getRecords();
		List<BasicAlarmContactsVO> basicAlarmContactsVOS = new ArrayList<>();

		List<Long> deptIds = remoteDeptUtils.getPropertyList(records, BasicAlarmContactsEntity::getDeptId);

		Map<Long, String> deptNameMap = remoteDeptUtils.getDeptNames(deptIds);

		for (BasicAlarmContactsEntity record : records) {
			BasicAlarmContactsVO vo = new BasicAlarmContactsVO();
			BeanUtils.copyProperties(record, vo);

			Long deptId = record.getDeptId();
			if (deptId != null) {
				String deptName = deptNameMap.get(deptId);
				vo.setDeptName(deptName);
			}
			basicAlarmContactsVOS.add(vo);
			int installCount = installLocation.selectCount(new LambdaQueryWrapper<BasicInstallLocationEntity>()
					.eq(BasicInstallLocationEntity::getAlarmWorkerId, record.getId())).intValue();
			record.setInstallCount(installCount);
		}
		this.updateBatchById(records);
		Page<BasicAlarmContactsVO> voPage = new Page<>(basicAlarmContactsPage.getCurrent(), basicAlarmContactsPage.getSize());
		voPage.setTotal(basicAlarmContactsPage.getTotal());
		voPage.setRecords(basicAlarmContactsVOS);

		return voPage;

	}

	@Override
	public List<BasicAlarmContactsEntity> getBasicAlarmContactsList(BasicAlarmContactsEntity basicAlarmContactsEntity) {
		LambdaQueryWrapper<BasicAlarmContactsEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(BasicAlarmContactsEntity::getDeptId, basicAlarmContactsEntity.getDeptId());
		List<BasicAlarmContactsEntity> basicAlarmContactsList = this.getBaseMapper().selectList(wrapper);
		return basicAlarmContactsList;
	}

	@Override
	public BasicAlarmContactsVO getVoById(Long id) {
		BasicAlarmContactsEntity basicAlarmContacts = this.getBaseMapper().selectById(id);

		BasicAlarmContactsVO basicAlarmContactsVO = new BasicAlarmContactsVO();
		BeanUtils.copyProperties(basicAlarmContacts, basicAlarmContactsVO);

		Long deptId = basicAlarmContacts.getDeptId();
		if (deptId != null) {
			String deptName = remoteDeptUtils.getDeptName(deptId);
			basicAlarmContactsVO.setDeptName(deptName);
		}
		return basicAlarmContactsVO;
	}
}